package cn.iocoder.yudao.module.cmt.service.content;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.content.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.content.ContentFeedbackDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cmt.dal.mysql.content.ContentFeedbackMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

@Service
@Validated
public class ContentFeedbackServiceImpl implements ContentFeedbackService {

    @Resource
    private ContentFeedbackMapper contentFeedbackMapper;

    @Override
    public Long createFeedback(ContentFeedbackSaveReqVO createReqVO) {
        ContentFeedbackDO feedback = BeanUtils.toBean(createReqVO, ContentFeedbackDO.class);
        contentFeedbackMapper.insert(feedback);
        return feedback.getId();
    }

    @Override
    public void updateFeedback(ContentFeedbackSaveReqVO updateReqVO) {
        validateFeedbackExists(updateReqVO.getId());
        ContentFeedbackDO updateObj = BeanUtils.toBean(updateReqVO, ContentFeedbackDO.class);
        contentFeedbackMapper.updateById(updateObj);
    }

    @Override
    public void deleteFeedback(Long id) {
        validateFeedbackExists(id);
        contentFeedbackMapper.deleteById(id);
    }

    private void validateFeedbackExists(Long id) {
        if (contentFeedbackMapper.selectById(id) == null) {
            throw exception(FEEDBACK_NOT_EXISTS);
        }
    }

    @Override
    public ContentFeedbackDO getFeedback(Long id) {
        return contentFeedbackMapper.selectById(id);
    }

    @Override
    public PageResult<ContentFeedbackDO> getFeedbackPage(ContentFeedbackPageReqVO pageReqVO) {
        return contentFeedbackMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteFeedbackListByIds(List<Long> ids) {
        ids.forEach(this::validateFeedbackExists);
        contentFeedbackMapper.deleteBatchIds(ids);
    }

}
